﻿using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Service.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LuShanTea.Service
{
    public class LogService : ILogService
    {
        // 记录该用户的操作
        public long AddNew(LogDTO log)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                // 日志不管是否重复都应该记录下来
                BaseService<Log> bs = new BaseService<Log>(ctx);
                Log logger = new Log();
                logger.AdminUserId = log.AdminUserId;
                logger.Describle = log.Describle;
                ctx.Logs.Add(logger);
                ctx.SaveChanges();
                return logger.Id;
                
            }
        }
        // 获取所有的日志记录
        public LogDTO[] GetAll()
        {

            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Log> bs = new BaseService<Log>(ctx);
                var data = bs.GetAll();
                if(data==null)
                {
                    return null;
                }
                return data.Include(l => l.AdminUser).AsNoTracking().ToList().Select(i => ToDTO(i)).ToArray();
            }
        }

        // 根据用户id获取他操作的记录
        public LogDTO[] GetByAdminUserId(long adminUserId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Log> bs = new BaseService<Log>(ctx);
                var data = bs.GetAll().Where(i => i.AdminUserId == adminUserId);
                if(data==null)
                {
                    throw new ArgumentException($"用户id为{adminUserId}的用户还没有对数据进行操作哦~~");
                }
                return data.Include(i => i.AdminUser).AsNoTracking().ToList().Select(i => ToDTO(i)).ToArray();
            }
        }

        public void MakeDelete(long logId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Log> bs = new BaseService<Log>(ctx);
                bs.MakeDelete(logId);
            }
        }
        // 更新用户操作的日志
        public void Updata(LogDTO log)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Log> bs = new BaseService<Log>(ctx);
                var data = bs.GetById(log.Id);
                if (data == null)
                {
                    throw new ArgumentException($"没有找到{log.AdminUserId}操作过的日志哦");
                }
                data.AdminUserId = log.AdminUserId;
                data.Describle = log.Describle;
                data.Id = log.Id;

                ctx.SaveChanges();
                
            }
        }

        public LogDTO ToDTO(Log log)
        {
            LogDTO dto = new LogDTO();
            dto.AdminUserName = log.AdminUser.Name;
            dto.AdminUserId = log.AdminUserId;
            dto.CreateDateTime = log.CreateDateTime;
            dto.Describle = log.Describle;
            dto.Id = log.Id;
            return dto;
        }

        public LogDTO GetById(long logId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Log> bs = new BaseService<Log>(ctx);
                var data = bs.GetById(logId);
                if(data==null)
                {
                    throw new ArgumentException($"id为{logId}的用户不存在");
                }
                return ToDTO(data);
            }
        }
    }
}
